<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/3/6
 * Time: 下午2:17
 */
class Goods_model extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
        $this->load->helper("common");
    }

    // 获取商品详情
    public function goods_detail($product_id)
    {

        // 调商城 接口

        return true;
    }

    // 优质办公用品TOP10
    public function company_office_supply($admin_shop_user_id)
    {
        $url = $this->config->item("shop_url") . 'companymanagerapi/company_office_supply/';
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        if(isset($admin_shop_user_id) && $admin_shop_user_id!=""){
            $data_arr['shop_user_id'] = $admin_shop_user_id;
        }
        $rs = curl_request($url, $data_arr);
        $rs = json_decode($rs, true);
        if ($rs['code'] == 200) {
            return $rs['data'];
        } else {
            return false;
        }
    }

    // 企业站获取分类
    public function company_goods_classify($post_arr)
    {
        $url = $this->config->item("shop_url") . 'companymanagerapi/company_goods_classify/';
        if (check_param($post_arr, ['pid'])) {
            $data_arr['pid'] = $post_arr['pid'];
            $data_arr['time'] = time();
            $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
            $rs = curl_request($url, $data_arr);
            $rs = json_decode($rs, true);
            if ($rs['code'] == 200) {
                return $rs['data'];
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    //商品详情多个id
    public function company_goods_infoes($out_single_ids_str,$admin_shop_user_id)
    {
        $url = $this->config->item('shop_url') . 'companymanagerapi/company_goods_infoes/';
        $data_arr['goodsids'] = $out_single_ids_str;
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item('token_const_wjyh') . $data_arr['time']);
        $data_arr['shop_user_id'] = $admin_shop_user_id;
        $rs = curl_request($url, $data_arr);
        $result = json_decode($rs, true);
        if ($result['code'] == 200) {
            return $result['data'];
        } else {
            return false;
        }
    }

    // 优质办公用品TOP10
    public function company_goods_brand()
    {
        $url = $this->config->item("shop_url") . 'companymanagerapi/company_goods_brand/';
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        $rs = curl_request($url, $data_arr);
        $rs = json_decode($rs, true);
        if ($rs['code'] == 200) {
            return $rs['data'];
        } else {
            return false;
        }
    }

    /*
     * 请求商城商品详情接口接口，并且对应在购物车中的数量
     * $ids 商品的子id，以'，'连接的字符串，例如1,2,3,4
     * $cart_goods 购物车中的信息，二维数组
     */
    public function get_shop_goods_info_sync_cart($ids, $cart_goods,$admin_shop_user_id)
    {
        //请求商城接口
        $goodsinfo = $this->company_goods_infoes($ids,$admin_shop_user_id);        
        if ($goodsinfo) {
            foreach ($goodsinfo as $key => $value) {
                foreach ($cart_goods as $k => $v) {
                    if ($v['product_id'] == $value['goods_id']) {
                        $data['data'][$key] = $cart_goods[$k];
                        $data['data'][$key][] = $value;
                    }
                }
            }
        } else {
            return false;
        }
        return $data['data'];
    }


    /*
     * 请求商城商品详情接口接口，并且对应在购物车中的数量
     * $ids 商品的子id，以'，'连接的字符串，例如1,2,3,4
     * $cart_goods 购物车中的信息，二维数组
     * $type 0为直接下单 1为购物车下单
     */
    public function get_shop_goods_info_sync_cart_type($ids, $type, $cart_goods,$admin_shop_user_id)
    {
        //请求商城接口
        $goodsinfo = $this->company_goods_infoes($ids,$admin_shop_user_id);
        if($type==1){
            if ($goodsinfo) {
                foreach ($goodsinfo as $key => $value) {
                    foreach ($cart_goods as $k => $v) {
                        if ($v['product_id'] == $value['goods_id']) {
                            $data['data'][$key] = $cart_goods[$k];
                            $data['data'][$key][] = $value;
                        }
                    }
                }
            } else {
                return false;
            }
        }else{
            $data['data'] = [];
            if($goodsinfo){
                foreach ($cart_goods[0] as $key => $value) {
                    $num = $value;
                }
                foreach ($goodsinfo as $key => $value) {
                    $data['data'][0]['num'] = $num;
                    $data['data'][0][] = $value;
                }
                
                
            }
        }
        return $data['data'];
    }

    /*
     * 请求商城商品详情接口接口
     */
    public function get_shop_goods_info($id,$admin_shop_user_id)
    {
        //请求商城接口
        $goodsinfo = $this->company_goods_infoes($id,$admin_shop_user_id);
        if($result['code']==200){
            $data['data']=$goodsinfo;
            return $data['data'];
        }else{
           return false;
        }
    }

}